package newtifyme.server;
//import com.google.gdata.client.*;
import com.google.gdata.client.youtube.*;
//import com.google.gdata.data.*;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.geo.impl.*;
//import com.google.gdata.data.media.*;
import com.google.gdata.data.media.mediarss.*;
import com.google.gdata.data.youtube.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class YtSearch {
	private static String key = "AI39si44RG2PRFgMUncXsepdm6wiGQRWLlW4EzeZwmAlc1kclp6LP_yhzRE0qC_cI1egp0sgopFHmibcgtOo8146f4fxoqNiJA";
	static YouTubeService service = new YouTubeService("local",key);
	/**
	 * @param args
	 * @throws ServiceException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException, ServiceException {
		// TODO Auto-generated method stub

		
		YouTubeQuery query = new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos"));
		// order results by the number of views (most viewed first)
		query.setOrderBy(YouTubeQuery.OrderBy.PUBLISHED);
		query.setTime(YouTubeQuery.Time.TODAY);
		
		
		
		// search for puppies and include restricted content in the search results
		query.setFullTextQuery("manu");
		query.setSafeSearch(YouTubeQuery.SafeSearch.NONE);

		VideoFeed videoFeed = service.query(query, VideoFeed.class);
		findLatestVideos(videoFeed,true);
		printVideoFeed(videoFeed, true);
		System.out.println(DateTime.parseDateTime("2011-04-13T02:30:38.000Z").toString());
	}
	public static void findLatestVideos(VideoFeed videoFeed, boolean detailed) {
	
		  for(VideoEntry videoEntry : videoFeed.getEntries() ) {
		
		    printLast(videoEntry, detailed);
		    
		  }
	}
	
	
	
	public static void printVideoFeed(VideoFeed videoFeed, boolean detailed) {
		  for(VideoEntry videoEntry : videoFeed.getEntries() ) {
		    printVideoEntry(videoEntry, detailed);
		  }
		}
	
	public static void printVideoEntry(VideoEntry videoEntry, boolean detailed) {
	  System.out.println("Title: " + videoEntry.getTitle().getPlainText());

	  if(videoEntry.isDraft()) {
	    System.out.println("Video is not live");
	    YtPublicationState pubState = videoEntry.getPublicationState();
	    if(pubState.getState() == YtPublicationState.State.PROCESSING) {
	      System.out.println("Video is still being processed.");
	    }
	    else if(pubState.getState() == YtPublicationState.State.REJECTED) {
	      System.out.print("Video has been rejected because: ");
	      System.out.println(pubState.getDescription());
	      System.out.print("For help visit: ");
	      System.out.println(pubState.getHelpUrl());
	    }
	    else if(pubState.getState() == YtPublicationState.State.FAILED) {
	      System.out.print("Video failed uploading because: ");
	      System.out.println(pubState.getDescription());
	      System.out.print("For help visit: ");
	      System.out.println(pubState.getHelpUrl());
	    }
	  }

	  if(videoEntry.getEditLink() != null) {
	    System.out.println("Video is editable by current user.");
	  }

	  if(detailed) {

	    YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();

	    System.out.println("         Date:" +mediaGroup.getUploaded());

	  }
	}

	public static void printLast(VideoEntry videoEntry, boolean detailed) {
	  

	  if(videoEntry.isDraft()) {
	    System.out.println("Video is not live");
	    YtPublicationState pubState = videoEntry.getPublicationState();
	    if(pubState.getState() == YtPublicationState.State.PROCESSING) {
	      System.out.println("Video is still being processed.");
	    }
	    else if(pubState.getState() == YtPublicationState.State.REJECTED) {
	      System.out.print("Video has been rejected because: ");
	      System.out.println(pubState.getDescription());
	      System.out.print("For help visit: ");
	      System.out.println(pubState.getHelpUrl());
	    }
	    else if(pubState.getState() == YtPublicationState.State.FAILED) {
	      System.out.print("Video failed uploading because: ");
	      System.out.println(pubState.getDescription());
	      System.out.print("For help visit: ");
	      System.out.println(pubState.getHelpUrl());
	    }
	  }

	  if(videoEntry.getEditLink() != null) {
	    System.out.println("Video is editable by current user.");
	  }

	  if(detailed) {
		  
	    YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
	    if (compareDates("2011-04-12T05:54:28.000Z", mediaGroup.getUploaded().toString())) {
	    	System.out.println("Title: " + videoEntry.getTitle().getPlainText());
	    	System.out.println( mediaGroup.getUploaded().toString());
	    }
	  }
	  
	}
	/**
	 * checks if data1 is older than date2
	 * @param date1
	 * @param date2
	 */
	public static boolean compareDates(String date1, String date2) {
		//compare year
		int year1 = Integer.parseInt(date1.split("-")[0]);
		int year2 = Integer.parseInt(date2.split("-")[0]);
		if (year1 > year2)
			return false;
		else {
			int month1 = Integer.parseInt(date1.split("-")[1]);
			int month2 = Integer.parseInt(date2.split("-")[1]);
			if (month1 > month2)
				return false;
			else {
				int datee1 = Integer.parseInt(date1.split("-")[2].split("T")[0]);
				int datee2 = Integer.parseInt(date2.split("-")[2].split("T")[0]);
				if (datee1>datee2)
					return false;
				else {
					date1 = date1.split("T")[1];
					date2 = date2.split("T")[1];
					
					int hour1 = Integer.parseInt(date1.split(":")[0]);
					//System.out.println(hour1);
					
					int hour2 = Integer.parseInt(date2.split(":")[0]);
					
					if ( hour1> hour2) return false;
					else if (hour1< hour2) return true;
					else {
						//System.out.println(hour2);
						int min1= Integer.parseInt(date1.split(":")[1]);
						int min2= Integer.parseInt(date2.split(":")[1]);
						if (min1>min2) {
							return false;
						} else {
							return true;
						}
					}
				}
			}
		}
			
	}
}